home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / c-tools-.000 / c-tools- / c-tools-0.4 / hsort.l < prev    next >
Encoding:
Lex Description  |  1995-08-13  |  3.5 KB  |  181 lines

  1. %{
  2.  
  3. /* hsort.l -- sort #include directives.  -*- C -*-
  4.    Copyright (C) 1995 Sandro Sigala - <sansig@freenet.hut.fi> */
  5.  
  6. /* $Id: hsort.l,v 1.15 1995/08/08 11:52:19 sandro Exp $ */
  7.  
  8. /* This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. #include "version.h"
  24.  
  25. #if REPLACE_YYWRAP == 1
  26. #undef yywrap
  27. #define yywrap() 1
  28. #endif
  29.  
  30. #define MAX_HEADERS    48
  31. #define MAX_SIZE    36
  32.  
  33. #include <ctype.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37.  
  38. int headers_s_idx = 0;
  39. int headers_l_idx = 0;
  40.  
  41. char headers_s[MAX_HEADERS][MAX_SIZE];
  42. char headers_l[MAX_HEADERS][MAX_SIZE];
  43.  
  44. void add_header_s (char *);
  45. void add_header_l (char *);
  46. void flush_headers_s (void);
  47. void flush_headers_l (void);
  48. %}
  49.  
  50. hvalid_char                [^">]
  51.  
  52. %%
  53.  
  54. "#include <"{hvalid_char}+">\n"        {
  55.                         char buf[128];
  56.  
  57.                         flush_headers_l ();
  58.                         strcpy (buf, yytext + 10);
  59.                         buf[strlen (buf) - 2] = 0;
  60.                         add_header_s (buf);
  61.                     }
  62.  
  63. "#include "["]{hvalid_char}+["]"\n"    {
  64.                         char buf[128];
  65.  
  66.                         flush_headers_s ();
  67.                         strcpy (buf, yytext + 10);
  68.                         buf[strlen (buf) - 2] = 0;
  69.                         add_header_l (buf);
  70.                     }
  71.  
  72. .*\n                    {
  73.                         flush_headers_s ();
  74.                         flush_headers_l ();
  75.                         ECHO;
  76.                     }
  77.  
  78. %%
  79.  
  80. int
  81. sort_function (const void *a, const void *b)
  82. {
  83.     return (strcmp ((char *) a, (char *) b));
  84. }
  85.  
  86. void
  87. add_header_s (char *s)
  88. {
  89.     strcpy (headers_s[headers_s_idx++], s);
  90. }
  91.  
  92. void
  93. add_header_l (char *s)
  94. {
  95.     strcpy (headers_l[headers_l_idx++], s);
  96. }
  97.  
  98. void
  99. flush_headers_s (void)
  100. {
  101.     int i = 0;
  102.  
  103.     qsort ((void *) headers_s, headers_s_idx,
  104.        sizeof (headers_s[0]), sort_function);
  105.  
  106.     while (i < headers_s_idx)
  107.     fprintf (yyout, "#include <%s>\n", headers_s[i++]);
  108.  
  109.     headers_s_idx = 0;
  110. }
  111.  
  112. void
  113. flush_headers_l (void)
  114. {
  115.     int i = 0;
  116.  
  117.     qsort ((void *) headers_l, headers_l_idx,
  118.        sizeof (headers_l[0]), sort_function);
  119.  
  120.     while (i < headers_l_idx)
  121.     fprintf (yyout, "#include \"%s\"\n", headers_l[i++]);
  122.  
  123.     headers_l_idx = 0;
  124. }
  125.  
  126. void
  127. helppage (char *progname)
  128. {
  129.     fprintf (stderr, "\
  130. %s %s - c-tools %s - Copyright (C) 1995 Sandro Sigala.\n\
  131. usage: %s [-h] [input [output]]\n\
  132.        -h        display this help and exit\n\
  133. ", progname, VERSION_HSORT, VERSION_CTOOLS, progname);
  134.     exit (0);
  135. }
  136.  
  137. int
  138. main (int argc, char *argv[])
  139. {
  140.     yyin = stdin;
  141.     yyout = stdout;
  142.  
  143.     if (argc > 3)
  144.     helppage (argv[0]);
  145.  
  146.     if (argc > 1)
  147.     {
  148.     if (strcmp (argv[1], "-h") == 0)
  149.         helppage (argv[0]);
  150.  
  151.     if (strcmp (argv[1], "-") != 0)
  152.         yyin = fopen (argv[1], "r");
  153.  
  154.     if (argc > 2)
  155.         if (strcmp (argv[2], "-") != 0)
  156.         yyout = fopen (argv[2], "w");
  157.     }
  158.  
  159.     if (yyin == NULL)
  160.     {
  161.     fprintf (stderr, "%s: cannot open input file\n", argv[0]);
  162.     exit (1);
  163.     }
  164.  
  165.     if (yyout == NULL)
  166.     {
  167.     fprintf (stderr, "%s: cannot open output file\n", argv[0]);
  168.     exit (1);
  169.     }
  170.  
  171.     while (yylex ())
  172.     ;
  173.  
  174.     flush_headers_s ();
  175.     flush_headers_l ();
  176.  
  177.     return 0;
  178. }
  179.  
  180. /* hsort.l ends here */
  181.